# -*- coding: utf-8 -*-
# @Time    : 2025/1/8 下午2:17
# @Author  : ysj
# @FileName: 工位检测.py
# @Software: PyCharm
# @Blog    ：https://blog.csdn.net/ydscc?type=blog
# import cv2
# import threading
# # RTSP视频流地址
# def rtsp_video():
#
#     rtsp_url = ("rtsp://10.10.30.65:8554/video")
#
#     # 创建VideoCapture对象，指定RTSP流地址
#     cap = cv2.VideoCapture(rtsp_url)
#
#     cap.set(cv2.CAP_FFMPEG, 1000)
#
#     while True:
#
#         # 读取一帧图像
#         ret, frame = cap.read()
#
#         if not ret:
#             print("Failed to read frame from RTSP stream. Check the stream URL and connectivity.")
#             break
#         # 如果成功读取到帧，则显示图像
#         cv2.imshow("RTSP Video Stream", frame)
#
#         cv2.waitKey(1)
#
#     # 释放VideoCapture资源
#     cap.release()
#     # 关闭所有OpenCV窗口
#     cv2.destroyAllWindows()
#
# thread = threading.Thread(target=rtsp_video, args=())
# thread.start()
#
import cv2
import mediapipe as mp




def is_box_in_box(box1, box2):
    """
    判断 box1 是否完全在 box2 内

    box1 和 box2 都是 (x_min, y_min, x_max, y_max) 格式的元组
    """
    x_min1, y_min1, x_max1, y_max1 = box1
    x_min2, y_min2, x_max2, y_max2 = box2

    # 判断 box1 和 box2 是否在水平方向和垂直方向上都有交集
    if max(x_min1, x_min2) < min(x_max1, x_max2) and max(y_min1, y_min2) < min(y_max1, y_max2):
        return True
    else:
        return False


rtsp_url = ("rtsp://admin:zx123456@192.168.1.63:554/h264/ch1/main/av_stream")

cap = cv2.VideoCapture('C:\Al\Software\AI_Model\demo\Recevie_Video_Data\lixiang_demo\output\output_video1.avi')

if not cap.isOpened():
    print("Could not open RTSP stream.")
    exit(-1)
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
boxroi = (1000, 100, 1640, 580)
x, y, w, h = 1000, 100, 640, 480
text = "On duty"
text1= "Leave  Post"
 # 文本位置 (x, y)
font = cv2.FONT_HERSHEY_SIMPLEX  # 字体类型
font_scale = 3  # 字体大小
color = (255, 255, 0)  # 字体颜色 (BGR)，此处为绿色
thickness = 5  # 字体线宽
is_leave = True
while True:
    ret, frame = cap.read()
    if not ret:
        print("Failed to read frame.")
        break
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)  # 绘制一个绿色矩形框
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.1) as face_detection:
        results = face_detection.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        if results.detections:
            for detection in results.detections:

                mp_drawing.draw_detection(frame, detection)
    cv2.imshow("Image", frame)
    cv2.waitKey(1)

cv2.destroyAllWindows()

    # 存储检测到的人员框信息
    # person_boxes = [(x, y, x + w, y + h) for (x, y, w, h) in faces]
    # frame1 = frame.copy()
    # # 在原帧上绘制矩形框
    # if person_boxes:
    #     for box in person_boxes:
    #         if is_box_in_box(box,boxroi):
    #             cv2.rectangle(frame1, (box[0], box[1]), (box[2], box[3]), (0, 0, 255), 2)
    #             # 在图像上绘制文本
    #             cv2.putText(frame1, text,  (box[0], box[1]), font, font_scale, color, thickness)
    #             cv2.imshow('RTSP Video Stream with Person Detection1', frame1)
    #
    # else :
    #     cv2.putText(frame1, text1, (boxroi[0], boxroi[1]), font, font_scale, color, thickness)
    #     cv2.imshow('RTSP Video Stream with Person Detection1', frame1)
    #
    #
    #
    # if cv2.waitKey(1) & 0xFF == ord('q'):
    #     break